package com.bw.inspection.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.common.vm.BaseViewModel
import com.bw.inspection.entity.InspectionEntity
import com.bw.inspection.entity.InspectionEntityItem
import com.bw.inspection.entity.NoInspenctionEntityItem
import com.bw.inspection.model.InspectionRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class InspectionViewModel @Inject constructor(private val repo: InspectionRepository):BaseViewModel() {
    //列表
    val mInspectionSuccess = MutableLiveData<List<InspectionEntityItem>>()
    fun getInspectionData() {
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadInspectionData()
            if (res.code == 200) {
                mInspectionSuccess.postValue(res.data)
            } else {
                mError.postValue("请求失败${res.message}")
            }
        }
    }
    //添加
    val mAddInspectionSuccess = MutableLiveData<InspectionEntityItem>()
    fun addInspectionData(inspectionEntity: InspectionEntityItem) {
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadAddInspectionData(inspectionEntity)
            if (res.code == 200) {
                mAddInspectionSuccess.postValue(res.data)
            } else {
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    // 详情
    val mInspectionDetailSuccess = MutableLiveData<InspectionEntityItem>()
    fun getInspectionDetailData(id:Int) {
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadInspectionDetailData(id)
            if (res.code == 200) {
                mInspectionDetailSuccess.postValue(res.data)
            } else {
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    //巡检代办
    val mPatrolSuccess = MutableLiveData<List<NoInspenctionEntityItem>>()
    fun getNoInspectionData(status:String) {
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadPatrolTaskData(status)
            if (res.code == 200) {
                mPatrolSuccess.postValue(res.data)
            } else {
                mError.postValue("请求失败${res.message}")
            }
        }
    }

    //巡检详情
    val mPatrolDetailSuccess = MutableLiveData<NoInspenctionEntityItem>()
    fun getPatrolTaskDetailData(id:Int) {
        viewModelScope.launch(mDispatcher) {
            val res = repo.loadPatrolTaskDetailData(id)
            if (res.code == 200) {
                mPatrolDetailSuccess.postValue(res.data)
            } else {
                mError.postValue("请求失败${res.message}")
            }
        }
    }
}